<?php
namespace app\admin\controller;

use app\model\ArticleModel;
use app\model\CategoryModel;
use think\facade\Request;
use think\facade\Validate;
use think\facade\View;

class Article extends Quit
{
    public function article_list()
    {
        parse_str(Request::query(), $queryParams);
        $cateforyList = ArticleModel::paginate([
            'list_rows' => 2,
            'query' => $queryParams
        ]);
        foreach ($cateforyList as $row) {

            $name = CategoryModel::find($row['category_id']);
            $row['category_name'] = $name['category_name'];
        }

//        unset($row);


        View::assign('cateforyList', $cateforyList);
        return View::fetch();

    }

    //增加分类
    public function add(){
        $cateforyList = CategoryModel::select();
        View::assign('cateforyList',$cateforyList);
        return View::fetch();
    }
    public function add_save(){
        $article = Request::param();
        $validate = Validate::rule([
            'article_title|文章标题' => 'require|min:2|max:50',
            'content|文章内容' => 'require|min:2|max:225',
            'category_id|分类编号' => 'require|between:1,'.PHP_INT_MAX,
        ]);
        $check = $validate->check($article);

        if(!$check){
            echo $validate->getError();
            echo '<a href="index.php?s=admin/article/article_list">返回主页</a>';
            exit();
        }

        $result = ArticleModel::create([
            'category_id' => $article['category_id'],
            'article_title' => $article['article_title'],
            'content' => $article['content'],
            'update_time' => time(),
            'add_time' => time()
        ]);
        if($result){
            echo "<a href='index.php?s=admin/article/article_list'>操作成功,返回首页</a>";
            exit();
        }else{
            echo "<a href='index.php?s=admin/article/article_list'>操作失败,返回首页</a>";
            exit();
        }
    }

    //编辑
    public function update(){
        $article = Request::param('article_id');
        $validate = Validate::rule([
            'article_id' => 'require|between:1,'.PHP_INT_MAX
        ]);
        $check = $validate ->check(['article_id' => $article]);
        if(!$check){
            echo $validate->getError();
            echo "<a href='index.php?s=admin/article/article_list'>返回首页</a>";
            exit();
        }
        $cateforyList = ArticleModel::find($article);
        if(empty($cateforyList  )){
            echo "<a href='index.php?s=admin/article/article_list'>该用户不存在，请返回首页</a>";
            exit();
        }
        $catefory = CategoryModel::select();
        if(empty($catefory)){
            echo "<a href='index.php?s=admin/article/article_list'>该用户数据有错，请返回首页</a>";
            exit();
        }
        View::assign('catefory',$catefory);
        View::assign('cateforyList',$cateforyList);
        return View::fetch();
    }
    public function update_save(){
        $article = Request::param();
        $validate = Validate::rule([
            'article_title|文章标题' => 'require|min:2|max:45',
            'content|文章内容' => 'require|min:2|max:225',
            'category_id|分类编号' => 'require|between:1,'.PHP_INT_MAX,
            'article_id|文章编号' =>'require|between:1,'.PHP_INT_MAX
        ]);
        $check = $validate ->check($article);
        if(!$check){
            echo $validate ->getError();
            echo "<a href='index.php?s=admin/article/article_list'>返回首页</a>";
            exit();
        }
        $result = ArticleModel::find($article['article_id']);
        if(empty($result)){
            echo "<a href='index.php?s=admin/article/article_list'>操作失败，返回首页</a>";
            exit();
        }
        $result['article_title'] = $article['article_title'];
        $result['category_id'] = $article['category_id'];
        $result['content'] = $article['content'];
        $result['uodate_time'] = time();
        $str = $result->save();
        if($str){
            echo "<a href='index.php?s=admin/article/article_list'>操作成功,返回首页</a>";
            exit();
        }else{
            echo "<a href='index.php?s=admin/article/article_list'>操作失败，返回首页</a>";
        }
    }

    //删除

    public function delete(){
        $articleid = Request::param('article_id');
        $validate = Validate::rule([
            'article_id|文章编号' => 'require|between:1,' . PHP_INT_MAX
        ]);
        $check = $validate->check(['article_id'=>$articleid]);
        if(!$check){
            echo $validate->getError();
            echo "<a href='index.php?s=admin/article/article_list'>返回首页</a>";
            exit();
        }
        $result = ArticleModel::find($articleid);
        if(empty($result)){
            echo "<a href='index.php?s=admin/article/article_list'>数据不对，返回首页</a>";
            exit();
        }
        $str = $result->delete();
        if($str){
            echo "<a href='index.php?s=admin/article/article_list'>操作成功，返回首页</a>";
            exit();
        }else{
            echo "<a href='index.php?s=admin/article/article_list'>操作失败，返回首页</a>";
            exit();
        }
    }

    //退出登录
    public function quit(){
        echo $this->quit_list();
    }

    //删除选中内容
    public function omit(){
        $article = Request::param();
        $id = implode(',',$article['checkboxid']);
        $resule = ArticleModel::destroy($id);
        if($resule){
            echo "<a href='index.php?s=admin/article/article_list'>操作成功,返回文章主页</a>";
        }else{
            echo "<a href='index.php?s=admin/article/article_list'>操作失败,返回文章主页</a>";
        }
    }

}